package com.gi.profiler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

import com.gi.profiler.db.DatabaseHelper;
import com.gi.profiler.db.DatabaseHelperManager;
import com.gi.profiler.model.ConnectivitySettings;
import com.gi.profiler.model.DayEnum;
import com.gi.profiler.model.DisplaySettings;
import com.gi.profiler.model.Profile;
import com.gi.profiler.model.SoundSettings;
import com.gi.profiler.model.Spot;
import com.gi.profiler.model.SpotProfileTime;
import com.gi.profiler.model.VolumeSettings;
import com.gi.profiler.model.WayPoint;
import com.gi.profiler.utils.DataConstants;

public class DaoTestActivity extends Activity {

    private final String LOG_NAME = getClass().getName();

    /**
     * You'll need this in your class to cache the helper in the class.
     */
    private DatabaseHelper databaseHelper = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_location_profiler);
 
	DatabaseHelper dbHelper = DatabaseHelperManager.getHelper();
	
	try {

	    int waypointId = dbHelper.getWayPointDao().create(new WayPoint(5, 44));

	    int spotId = dbHelper.getSpotDao().create(
		    new Spot("TEST sPOT", dbHelper.getWayPointDao().queryForId(waypointId)));
	    int volId = dbHelper.getSoundSettingsDao().create(
		    new SoundSettings(new VolumeSettings(1,2,3,4,5,6), -1, null, null));

	    int profileId =dbHelper.getProfileDao().create(
		    new Profile("Test 1",DataConstants.IC_lOUD, dbHelper.getSoundSettingsDao().queryForId(volId), new ConnectivitySettings(),
			    new DisplaySettings()));
	    
	    dbHelper.getProfileDao().create(
		    new Profile("Test 2",DataConstants.IC_SILENT, dbHelper.getSoundSettingsDao().queryForId(volId), null,
			    new DisplaySettings()));
	    
	    dbHelper.getProfileDao().create(
		    new Profile("Test 3",DataConstants.IC_lOUD, dbHelper.getSoundSettingsDao().queryForId(volId),  new ConnectivitySettings(),
			    null));
	    
	    dbHelper.getProfileDao().create(
		    new Profile("Test 5",DataConstants.IC_lOUD, new SoundSettings(), new ConnectivitySettings(),
			    null));
	    
	    dbHelper.getSpotProfileTimeDao().create(
		    new SpotProfileTime(dbHelper.getProfileDao().queryForId(profileId),
			    dbHelper.getSpotDao().queryForId(spotId), new ArrayList<DayEnum>(
				    Arrays.asList(DayEnum.values()))));

	    SpotProfileTime sp = dbHelper.getSpotProfileTimeDao().queryForId(1);
	    Log.i(LOG_NAME, "================================");
	    Log.i(LOG_NAME, sp.toString());

	} catch (SQLException e) {
	    // TODO Auto-generated catch block
	    Log.e(LOG_NAME, e.getMessage());
	}
	
	DatabaseHelperManager.releaseHelper();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	getMenuInflater().inflate(R.menu.activity_location_profiler, menu);
	return true;
    }

//    @Override
//    protected void onDestroy() {
//	super.onDestroy();
//
//	/*
//	 * You'll need this in your class to release the helper when done.
//	 */
//	if (databaseHelper != null) {
//	    OpenHelperManager.releaseHelper();
//	    databaseHelper = null;
//	}
//    }
//
//    /**
//     * You'll need this in your class to get the helper from the manager once
//     * per class.
//     */
//    private DatabaseHelper getHelper() {
//	if (databaseHelper == null) {
//	    databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
//	}
//	return databaseHelper;
//    }
}
